#coding=utf-8
#LightCam 基本监控程序
#init by hfh 20150212

import socket,os,time,re,sys
import lc_libs
import multiprocessing,subprocess,traceback

def write_data(datastr):
	today=time.strftime('%Y%m%d',time.localtime())
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	datafile=open("../data/BASIC/"+today,"a")
	datafile.write(logdate+" "+datastr+"\n")
	datafile.close()

def get_uptime_user(strs):
	res=strs.split(",")
	upyear="0"
	upday="0"
	uphour="0"
	upmin="0"
	loguser="0"
	for line in res:
		tline=line.split()
		length=len(tline)
		if "years" in line:
			upyear=tline[length-2]
		elif "days" in line:
			upday=tline[length-2]
		elif "min" in line:
			upmin=tline[length-2]
		elif re.match("^top.*[0-9][0-9]:[0-9][0-9]$",line):
			templine=tline[length-1]
			tline=templine.split(":")
			uphour=tline[0].strip()
			upmin=tline[1]
		elif re.match(".*[0-9]:[0-9][0-9]$",line) and "up" not in line:
			tline=line.split(":")
			uphour=tline[0].strip()
			upmin=tline[1]
		elif "users" in line:
			loguser=tline[length-2]
	redata=[upyear,upday,uphour,upmin,loguser]
	return redata
			

if __name__ == "__main__":

	#初始化
	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()
	
	#comd = "top -b -n 1|head -20;df|grep ' /'|awk '{print \"DiskUsed \"$NF\" \"$(NF-1)}'"
	comd = "top -b -n 1;df|grep ' /'|awk '{print \"DiskUsed \"$NF\" \"$(NF-1)}'"
	
	Uptime=""
	UserLoggingOn=""
	TaskTotal=""
	TaskRunning=""
	TaskSleeping=""
	TaskStopped=""
	TaskZombie=""
	CpuUsedPercent=""
	CpuUsedPercentUser=""
	CpuUsedPercentSys=""
	MemUsedPercent=""
	MemTotal=""
	MemUsed=""
	MemFree=""
	MemBuffers=""
	SwapUsedPercent=""
	SwapTotal=""
	SwapUsed=""
	SwapFree=""
	SwapCached=""
	
	CpuUsedThreshold="85"
	MemUsedThreshold="95"
	ProcessCpuUsedThreshold="100"
	ProcessMemUsedThreshold="100"
	DiskUsedThreshold="75"
	CpuUsedSeverity="4"
	MemUsedSeverity="4"
	ProcessCpuUsedSeverity="3"
	ProcessMemUsedSeverity="3"
	DiskUsedSeverity="4"
	
	#读取阈值
	agent_hostname=lc_libs.lc_gethostname()
	try:
		for line in open(confname):
			sline=line.strip("\r\n").split("\t")
			if line[0] == '#' or line.split("\t")[0] not in (agent_hostname,'default'):
				continue
			elif sline[1]=="CpuUsedThreshold":
				CpuUsedThreshold=sline[2]
				CpuUsedSeverity=sline[3]
			elif sline[1]=="MemUsedThreshold":
				MemUsedThreshold=sline[2]
				MemUsedSeverity=sline[3]
			elif sline[1]=="ProcessCpuUsedThreshold":
				ProcessCpuUsedThreshold=sline[2]
				ProcessCpuUsedSeverity=sline[3]
			elif sline[1]=="ProcessMemUsedThreshold":
				ProcessMemUsedThreshold=sline[2]
				ProcessMemUsedSeverity=sline[3]
			elif sline[1]=="DiskUsedThreshold":
				DiskUsedThreshold=sline[2]
				DiskUsedSeverity=sline[3]
	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" 阈值配置文件读取失败:\n"+excinfo+"\n")
		logfile.flush()
		exit(0)
		
	#清理标志文件
	os.system("rm -f ../alert/lc_basic*")



	try:
		#初始化数据库
		
		db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

		db.connect()
		Conn=db.getConn()
		if Conn==None:
			print "Can not Connect to the MondbDB"
			exit(0)
		
		events_dbname='lc_events'
		events_tablename='lc_events'
		
		history_dbname='lc_history'
		history_tablename="lc_basic"
		
		events_db=db.useDatabase(events_dbname)
		events_table=db.UseCollection(events_db,events_tablename)
		
		history_db=db.useDatabase(history_dbname)
		history_table=db.UseCollection(history_db,history_tablename)

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" 初始化数据库失败:\n"+excinfo+"\n")
		logfile.flush()
		exit(0)
	
	#设置节点,告警组
	Node=agent_hostname
	AlertGroup="MonitorBasic"
	
	#主程序
	while True:
		try:
			today=time.strftime('%Y%m%d',time.localtime())
			popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#判断子进程是否结束
			while True:
				if popen.poll()==None:
					time.sleep(0.1)
					continue
				else:
					break
			#处理程序
			for line in popen.stdout.readlines():
				#系统启动时间与当前登录用户数
				if re.match("^top",line):
					res=get_uptime_user(line)
					Uptime=res[0]+" years, "+res[1]+" days, "+res[2]+" hours, "+res[3]+" mintues"
					uptimeTot=int(res[0])*365*24*60+int(res[1])*24*60+int(res[2])*60+int(res[3])
					UserLoggingOn=res[4]
					datas=[
						{"Node":Node,"Name":"Uptime","Value":Uptime,"Time":int(time.time())},
						{"Node":Node,"Name":"UserLoggingOn","Value":UserLoggingOn,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					if uptimeTot<=24*60:
						AlertKey="HostReboot" + today
						Severity="4"
						Type="1"
						Summary=Node+"系统 "+str(uptimeTot)+"分钟前发生重启 "
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				#系统进程数量
				elif re.match("^Task",line):
					res=line.strip("\n").split()
					TaskTotal=res[1]
					TaskRunning=res[3]
					TaskSleeping=res[5]
					TaskStopped=res[7]
					TaskZombie=res[9]
					datas=[
						{"Node":Node,"Name":"TaskTotal","Value":TaskTotal,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskRunning","Value":TaskRunning,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskSleeping","Value":TaskSleeping,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskStopped","Value":TaskStopped,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskZombie","Value":TaskZombie,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
				#CPU相关指标
				elif re.match("^Cpu",line):
					res=line.strip("\n").split(',')
					CpuUsedPercent=str(100-float(res[3].strip('%id').strip()))+"%"
					CpuUsedPercentUser=res[0].split(':')[1].strip('us').strip()
					CpuUsedPercentSys=res[1].strip('sy').strip()
					datas=[
						{"Node":Node,"Name":"CpuUsedPercent","Value":CpuUsedPercent,"Time":int(time.time())},
						{"Node":Node,"Name":"CpuUsedPercentUser","Value":CpuUsedPercentUser,"Time":int(time.time())},
						{"Node":Node,"Name":"CpuUsedPercentSys","Value":CpuUsedPercentSys,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskStopped","Value":TaskStopped,"Time":int(time.time())},
						{"Node":Node,"Name":"TaskZombie","Value":TaskZombie,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					#处理CpuUsedPercent
					if float(CpuUsedPercent.strip("%"))>=float(CpuUsedThreshold):
						AlertKey="CpuUsedPercent"
						Severity=CpuUsedSeverity
						Type="1"
						#Summary="Cpu Used Percent is "+str(CpuUsedPercent)+" , over than threshold "+str(CpuUsedThreshold)
						Summary=Node+"系统CPU使用率达到 "+str(CpuUsedPercent)+" , 超过了设定的阈值 "+str(CpuUsedThreshold)
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						AlertKey="CpuUsedPercent"
						Severity="1"
						Type="2"
						Summary=Node+"系统CPU使用为 "+str(CpuUsedPercent)+" , 低于设定的阈值 "+str(CpuUsedThreshold)
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				#内存相关指标
				elif re.match("^Mem",line):
					res=line.strip("\n").split()
					MemTotal=res[1]
					MemUsed=res[3]
					MemFree=res[5]
					MemBuffers=res[7]
					MemUsedPercent=str("%.2f" % (100*float(MemUsed.strip('k'))/float(MemTotal.strip('k'))))+"%"
					#print "MemUsedPercent:"+MemUsedPercent
					#print "MemTotal:"+MemTotal
					#print "MemUsed:"+MemUsed
					#print "MemFree:"+MemFree
					#print "MemBuffers:"+MemBuffers
					#write_data("MemUsedPercent:"+MemUsedPercent)
					#write_data("MemTotal:"+MemTotal)
					#write_data("MemUsed:"+MemUsed)
					#write_data("MemFree:"+MemFree)
					#write_data("MemBuffers:"+MemBuffers)
					#hdb.insert_historydb(Node,"MemUsedPercent",MemUsedPercent)
					#hdb.insert_historydb(Node,"MemTotal",MemTotal)
					#hdb.insert_historydb(Node,"MemUsed",MemUsed)
					#hdb.insert_historydb(Node,"MemFree",MemFree)
					#hdb.insert_historydb(Node,"MemBuffers",MemBuffers)
					datas=[
						{"Node":Node,"Name":"MemUsedPercent","Value":MemUsedPercent,"Time":int(time.time())},
						{"Node":Node,"Name":"MemTotal","Value":MemTotal,"Time":int(time.time())},
						{"Node":Node,"Name":"MemUsed","Value":MemUsed,"Time":int(time.time())},
						{"Node":Node,"Name":"MemFree","Value":MemFree,"Time":int(time.time())},
						{"Node":Node,"Name":"MemBuffers","Value":MemBuffers,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					#处理MemUsedPercent
					#if float(MemUsedPercent.strip("%"))>=float(MemUsedThreshold):
					#	AlertKey="MemUsedPercent"
					#	Severity=MemUsedSeverity
					#	Type="1"
					#	#Summary="Mem Used Percent is "+str(MemUsedPercent)+" , over than threshold "+str(MemUsedThreshold)
					#	Summary=Node+"系统内存使用率达到 "+str(MemUsedPercent)+" , 超过设定的阈值 "+str(MemUsedThreshold)
					#	strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					#	print strs
					#	db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					#	#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
					#	#logfile.write(logdate+" "+strs+"\n")
					#	#logfile.flush()
					#else:
					#	AlertKey="MemUsedPercent"
					#	Severity="1"
					#	Type="2"
					#	#Summary="Mem Used Percent is "+str(MemUsedPercent)+" , less than threshold "+str(MemUsedThreshold)
					#	Summary=Node+"系统内存使用率为 "+str(MemUsedPercent)+" , 低于设定的阈值 "+str(MemUsedThreshold)
					#	strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					#	print strs
					#	db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					#	#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
					#	#logfile.write(logdate+" "+strs+"\n")
					#	#logfile.flush()
				#SWAP相关指标
				elif re.match("^Swap",line):
					res=line.strip("\n").split()
					SwapTotal=res[1]
					SwapUsed=res[3]
					SwapFree=res[5]
					SwapCached=res[7]
					SwapUsedPercent=str("%.2f" % (100*float(SwapUsed.strip('k'))/float(SwapTotal.strip('k'))))+"%"
					#print "SwapUsedPercent:"+SwapUsedPercent
					#print "SwapTotal:"+SwapTotal
					#print "SwapUsed:"+SwapUsed
					#print "SwapFree:"+SwapFree
					#print "SwapCached:"+SwapCached
					#write_data("SwapUsedPercent:"+SwapUsedPercent)
					#write_data("SwapTotal:"+SwapTotal)
					#write_data("SwapUsed:"+SwapUsed)
					#write_data("SwapFree:"+SwapFree)
					#write_data("SwapCached:"+SwapCached)
					#hdb.insert_historydb(Node,"SwapUsedPercent",SwapUsedPercent)
					#hdb.insert_historydb(Node,"SwapTotal",SwapTotal)
					#hdb.insert_historydb(Node,"SwapUsed",SwapUsed)
					#hdb.insert_historydb(Node,"SwapFree",SwapFree)
					#hdb.insert_historydb(Node,"SwapCached",SwapCached)
					datas=[
						{"Node":Node,"Name":"SwapUsedPercent","Value":SwapUsedPercent,"Time":int(time.time())},
						{"Node":Node,"Name":"SwapTotal","Value":SwapTotal,"Time":int(time.time())},
						{"Node":Node,"Name":"SwapUsed","Value":SwapUsed,"Time":int(time.time())},
						{"Node":Node,"Name":"SwapFree","Value":SwapFree,"Time":int(time.time())},
						{"Node":Node,"Name":"SwapCached","Value":SwapCached,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
				#处理空行
				elif line.strip("\n")=="":
					continue
				#处理标题行
				elif re.match(".*PID.*USER.*COMMAND.*",line):
					continue
				#磁盘相关指标
				elif re.match("^DiskUsed",line):
					res=line.strip("\n").split()
					DiskName=res[1]
					DiskUsedPercent=res[2]
					#print "DiskName:"+DiskName
					#print "DiskUsed:"+DiskUsed
					#write_data("DiskName:"+DiskName+" DiskUsedPercent:"+DiskUsedPercent)
					#hdb.insert_historydb(Node,"DiskUsedPercent_"+DiskName,DiskUsedPercent)
					datas=[
						{"Node":Node,"Name":"DiskUsedPercent_"+DiskName,"Value":DiskUsedPercent,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					#处理DiskUsedPercent
					if float(DiskUsedPercent.strip("%"))>=float(DiskUsedThreshold):
						AlertKey="DiskUsed-"+DiskName
						Severity=DiskUsedSeverity
						Type="1"
						#Summary="Disk "+DiskName+" Used Percent is "+str(DiskUsedPercent)+" , over than threshold "+str(DiskUsedThreshold)
						Summary=Node+"系统磁盘 "+DiskName+" 当前使用率为 "+str(DiskUsedPercent)+" , 超过设定的阈值 "+str(DiskUsedThreshold)
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
					else:
						AlertKey="DiskUsed-"+DiskName
						Severity="1"
						Type="2"
						#Summary="Disk "+DiskName+" Used Percent is "+str(DiskUsedPercent)+" , less than the threshold "+str(DiskUsedThreshold)
						Summary=Node+"系统磁盘 "+DiskName+" 当前使用率为 "+str(DiskUsedPercent)+" , 低于设定的阈值 "+str(DiskUsedThreshold)
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
				#进程详细情况Top10
				else:
					res=line.strip("\n").split()
					#print "COMMAND: "+res[11]+" PID:"+res[0]+" USER:"+res[1]+" CPU:"+res[8]+"% MEM:"+res[9]+"% TIME:"+res[10]
					ProcessCpuUsed=res[8]
					ProcessMemUsed=res[8]
					#write_data("COMMAND:"+res[11]+" PID:"+res[0]+" USER:"+res[1]+" CPU:"+res[8]+"% MEM:"+res[9]+"% TIME:"+res[10])
					#hdb.insert_historydb(Node,"ProcessCpuUsed_"+res[11],ProcessCpuUsed)
					datas=[
						{"Node":Node,"Name":"ProcessCpuUsed_"+res[11],"Value":ProcessCpuUsed,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					datas=[
						{"Node":Node,"Name":"ProcessMemUsed"+res[11],"Value":ProcessMemUsed,"Time":int(time.time())}
						]
					db.insertDatas(history_table,datas)
					#处理ProcessCpuUsed
					if float(ProcessCpuUsed.strip("%"))>=float(ProcessCpuUsedThreshold):
						AlertKey="ProcessCPU-"+res[11]
						Severity=ProcessCpuUsedSeverity
						Type="1"
						#Summary="Process "+res[11]+" Cpu Used Percent is "+str(ProcessCpuUsed)+" , over than threshold "+str(ProcessCpuUsedThreshold)
						Summary=Node+"系统进程 "+res[11]+" 的CPU使用率达到 "+str(ProcessCpuUsed)+"% , 超过设定的阈值 "+str(ProcessCpuUsedThreshold)+"%"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
					else:
						AlertKey="ProcessCPU-"+res[11]
						Severity="1"
						Type="2"
						#Summary="Process "+res[11]+" Cpu Used Percent is "+str(ProcessCpuUsed)+" , less than threshold "+str(ProcessCpuUsedThreshold)
						Summary=Node+"系统进程 "+res[11]+" 的CPU使用率为 "+str(ProcessCpuUsed)+"% , 低于设定的阈值 "+str(ProcessCpuUsedThreshold)+"%"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
					#处理ProcessMemUsed
					if float(ProcessMemUsed.strip("%"))>=float(ProcessMemUsedThreshold):
						AlertKey="ProcessMEM-"+res[11]
						Severity=ProcessMemUsedSeverity
						Type="1"
						#Summary="Process "+res[11]+" Cpu Used Percent is "+str(ProcessCpuUsed)+" , over than threshold "+str(ProcessCpuUsedThreshold)
						Summary=Node+"系统进程 "+res[11]+" 的MEM使用率达到 "+str(ProcessMemUsed)+"% , 超过设定的阈值 "+str(ProcessMemUsedThreshold)+"%"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
					else:
						AlertKey="ProcessMEM-"+res[11]
						Severity="1"
						Type="2"
						#Summary="Process "+res[11]+" Cpu Used Percent is "+str(ProcessCpuUsed)+" , less than threshold "+str(ProcessCpuUsedThreshold)
						Summary=Node+"系统进程 "+res[11]+" 的MEM使用率为 "+str(ProcessMemUsed)+"% , 低于设定的阈值 "+str(ProcessMemUsedThreshold)+"%"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						#logfile.write(logdate+" "+strs+"\n")
						#logfile.flush()
			#处理MemUsedPercent
			MemUsedReal=float(MemUsed.strip('k'))-float(MemBuffers.strip('k'))-float(SwapCached.strip('k'))
			MemUsedPercent=str("%.2f" % (100*float(MemUsedReal)/float(MemTotal.strip('k'))))+"%"
			print MemUsedPercent
			
			if float(MemUsedPercent.strip("%"))>=float(MemUsedThreshold):
				AlertKey="MemUsedPercent"
				Severity=MemUsedSeverity
				Type="1"
				#Summary="Mem Used Percent is "+str(MemUsedPercent)+" , over than threshold "+str(MemUsedThreshold)
				Summary=Node+"系统内存使用率达到 "+str(MemUsedPercent)+" , 超过设定的阈值 "+str(MemUsedThreshold)
				strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				print strs
				db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
				#logfile.write(logdate+" "+strs+"\n")
				#logfile.flush()
			else:
				AlertKey="MemUsedPercent"
				Severity="1"
				Type="2"
				#Summary="Mem Used Percent is "+str(MemUsedPercent)+" , less than threshold "+str(MemUsedThreshold)
				Summary=Node+"系统内存使用率为 "+str(MemUsedPercent)+" , 低于设定的阈值 "+str(MemUsedThreshold)
				strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				print strs
				db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
				#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
				#logfile.write(logdate+" "+strs+"\n")
				#logfile.flush()
			
		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" "+excinfo+"\n")
			logfile.flush()
			time.sleep(1)
		time.sleep(300)
